package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import cn.lrwjz.androidguide.R;

public class RadioButtonActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);

        // 设置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("RadioButton 示例");
        }

        // 设置基础单选按钮组监听
        RadioGroup basicRadioGroup = findViewById(R.id.basicRadioGroup);
        basicRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            String selectedOption = "";
            if (checkedId == R.id.radio_option1) {
                selectedOption = "选项一";
            } else if (checkedId == R.id.radio_option2) {
                selectedOption = "选项二";
            } else if (checkedId == R.id.radio_option3) {
                selectedOption = "选项三";
            }
            Toast.makeText(this, "基础单选按钮组：选择了" + selectedOption, Toast.LENGTH_SHORT).show();
        });

        // 设置自定义样式单选按钮组监听
        RadioGroup customRadioGroup = findViewById(R.id.customRadioGroup);
        customRadioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            String selectedOption = "";
            if (checkedId == R.id.custom_radio1) {
                selectedOption = "自定义选项一";
            } else if (checkedId == R.id.custom_radio2) {
                selectedOption = "自定义选项二";
            }
            Toast.makeText(this, "自定义单选按钮组：选择了" + selectedOption, Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 